#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 18-4-10 下午3:00
# @Author  : huanggz
# @File    : cookieMiddleware.py
# @Software: PyCharm
# code is far away from bugs with the god animal protecting

from django.http import JsonResponse

from django.utils.deprecation import MiddlewareMixin
import base64
from pang5_web.func import get_uid
from pang5_web.settings import DEBUG


class CookieMiddleware(MiddlewareMixin):
    def process_request(self, request):
        # try:
        if not DEBUG:
            #     print("cookie 请求")
            cookie=request.COOKIES.get('pang5','')
            # cookie = 'MTA2MjU2Nw==='
            cookie=cookie.replace('%3D','=')
            if not cookie:
                return JsonResponse({'status': 403, 'msg': '用户未登录'})
            # uid = get_uid(cookie)
            uid=int(int(base64.b64decode(str(cookie)))/99-647)

            if uid:
                pass
            else:
                return JsonResponse({'status':403, 'msg': '用户未登录'})

    # except Exception as e:
    #     print(e)
    #     return JsonResponse({'error': 500, 'msg': 'cookie不存在    1W'})

    def process_respones(self, request, respones):
        # print("cookie 请求返回")
        pass


if __name__ == '__main__':
    pass
